<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use backend\modules\supplier\models\Storage;
use backend\modules\supplier\models\Rack;

/* @var $this yii\web\View */
/* @var $model backend\modules\supplier\models\Slot */
/* @var $form yii\widgets\ActiveForm */

$def = [0 => '请选择'];

$storages = ArrayHelper::map(Storage::getAll(), 'id', 'name');
$storages = array_merge($def, $storages);

$racks = !$model->isNewRecord && $model->storage_id > 0 ? ArrayHelper::map(Rack::getAll($model->storage_id), 'id', 'name') : [];
$racks = array_merge($def, $racks);
?>

<div class="slot-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => 100]) ?>

    <div>
        <legend>位置</legend>
        <div class="row">
            <div class="col-sm-2"><?= $form->field($model, 'storage_id')->dropDownList($storages) ?></div>
            <div class="col-sm-2"><?= $form->field($model, 'rack_id')->dropDownList($racks) ?></div>
        </div>
    </div>
    
    <div>
        <legend>体积</legend>
        <div class="row">
            <div class="col-sm-2"><?= $form->field($model, 'length')->textInput() ?></div>
            <div class="col-sm-2"><?= $form->field($model, 'width')->textInput() ?></div>
            <div class="col-sm-2"><?= $form->field($model, 'height')->textInput() ?></div>
        </div>
    </div>

    <?= $form->field($model, 'sort')->textInput() ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '新建' : '更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        
        <?php 
        if($model->isNewRecord){
            echo Html::submitButton('批量添加', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
        }
        ?>
        
    </div>

    <?php ActiveForm::end(); ?>

</div>

<script>
window.onload=function(){
    $('#slot-storage_id').change(function(){
        var url='<?= Url::to(['get-rack']) ?>';
        var storageId=$(this).val();
        $.getJSON(url,{storageId:storageId},function(ret){
            $('#slot-rack_id').empty();
            $.each(ret,function(i,item){
                $('#slot-rack_id').append('<option value="'+item.id+'">'+item.name+'</option>');
            });
        });
    });
};
</script>